package com.jumi.microservice.material.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.jumi.microservice.material.domain.JmDict;
import com.jumi.microservice.material.domain.JmDictDTO;
import com.jumi.microservice.material.domain.JmDictValue;
import com.jumi.microservice.material.mapper.JmDictMapper;
import com.jumi.microservice.material.mapper.JmDictValueMapper;
import com.jumi.microservice.material.service.JmDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ssk
 * @create 2020/8/4 10:35
 **/
@Service
public class JmDictServiceImpl implements JmDictService {

    @Resource
    private JmDictMapper jmDictMapper;

    @Resource
    private JmDictValueMapper jmDictValueMapper;

    @Override
    public List<JmDictDTO> getStockChangeTypes() {

        List<JmDictDTO> result = Lists.newArrayList();

        List<String> stockDictCodesList = Lists.newArrayList();
        stockDictCodesList.add("WH_IN_TYPE_NOR");
        stockDictCodesList.add("WH_IN_TYPE_UNNOR");
        stockDictCodesList.add("WH_ORDER_OUT_TYPE");
        stockDictCodesList.add("WH_TRANSFER_TYPE");
        stockDictCodesList.add("WH_CONVERSION_TYPE");
        stockDictCodesList.add("PUBLIC_GOODS_WH_OUT_TYPE");

        List<JmDict> dictList = jmDictMapper.selectList(
                Wrappers.<JmDict>lambdaQuery()
                        .select(JmDict::getDictId, JmDict::getDictCode, JmDict::getDictName)
                        .in(JmDict::getDictCode, stockDictCodesList)
        );
        if (dictList.size() == 0) {
            return result;
        }
        Map<Long, JmDict> dictMap = dictList.stream().collect(Collectors.toMap(JmDict::getDictId, k -> k));
        List<Long> dictIds = dictList.stream().map(JmDict::getDictId).collect(Collectors.toList());

        List<JmDictValue> dictValueList = jmDictValueMapper.selectList(
                Wrappers.<JmDictValue>lambdaQuery()
                        .select(JmDictValue::getValId, JmDictValue::getValName, JmDictValue::getDictId)
                        .in(JmDictValue::getDictId, dictIds)
                        .eq(JmDictValue::getIsDel,0)
        );
        if (dictValueList.size() == 0) {
            return result;
        }
        dictValueList.forEach(dictValue -> {
            JmDict dict = dictMap.get(dictValue.getDictId());
            JmDictDTO dictDTO = new JmDictDTO();
            dictDTO.setValId(dictValue.getValId());
            dictDTO.setValName(dictValue.getValName());
            dictDTO.setDictId(dict.getDictId());
            dictDTO.setDictCode(dict.getDictCode());
            dictDTO.setDictName(dict.getDictName());
            result.add(dictDTO);
        });

        return result;
    }

	@Override
	public List<JmDictValue> getDictValuesByDictId(Integer id)
	{
        return jmDictValueMapper.findListByDictId(Long.valueOf(id));
	}
}
